package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.entity.Contact;

public class ContactDao {

	com.util.DBHelp<com.entity.Contact> db = new com.util.DBHelp<com.entity.Contact>();
	
	private class ContactRowMapper implements com.util.RowMapper<com.entity.Contact>{

		public Contact mapperRow(ResultSet rs) throws SQLException {
			Contact c = new Contact();
			c.setId(rs.getString("id"));
			c.setName(rs.getString("name"));
			c.setCompanyName(rs.getString("companyname"));
			c.setMobile(rs.getString("mobile"));
			c.setTel(rs.getString("tel"));
			c.setEmail(rs.getString("email"));
			c.setAddress(rs.getString("address"));
			c.setWebsite(rs.getString("website"));
			c.setWeibo(rs.getString("weibo"));
			c.setContent(rs.getString("content"));
			c.setProjectId(rs.getString("projectId"));
			c.setCreateTime(rs.getString("createTime"));
			
			return c;
		}
		
	}

	public void addContent(Contact c) {
		String sql = "INSERT INTO t_contact(id,`name`,companyname,mobile,tel,email,address,website,weibo,content,projectid,createtime) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
		//int line = db.executeSQL(sql,c.getId(),c.getName(),c.getCompanyName(),c.getMobile(),c.getTel(),c.getEmail(),c.getAddress(),c.getWebsite(),c.getWeibo(),c.getContent(),c.getProjectId(),c.getCreateTime());
		db.executeSQL(sql,c.getId(),c.getName(),c.getCompanyName(),c.getMobile(),c.getTel(),c.getEmail(),c.getAddress(),c.getWebsite(),c.getWeibo(),c.getContent(),c.getProjectId(),c.getCreateTime());
	}



	public List<Contact> findAllByPid(String pid) {
		String sql = "SELECT `id`,`name`,`companyname`,`mobile`,`tel`,`email`,`address`,`website`,`weibo`,`content`,`projectid`,`createtime` FROM `t_contact` WHERE `projectid`=? ORDER BY `createtime` DESC";
		return db.executeQueryForList(sql, new ContactRowMapper(), pid);
	}



	public Contact getById(String id) {
		String sql="SELECT `id`,`name`,`companyname`,`mobile`,`tel`,`email`,`address`,`website`,`weibo`,`content`,`projectid`,`createtime` FROM `t_contact` WHERE `id`=? ORDER BY `createtime` DESC";
		return db.executeQueryForObject(sql, new ContactRowMapper() , id);
	}



	public void updateContact(Contact c) {
		String sql="UPDATE `t_contact` SET `name`=?,`companyname`=?,`mobile`=?,`tel`=?,`email`=?,`address`=?,`website`=?,`weibo`=?,`content`=? WHERE id=?";
		db.executeSQL(sql, c.getName(),c.getCompanyName(),c.getMobile(),c.getTel(),c.getEmail(),c.getAddress(),c.getWebsite(),c.getWeibo(),c.getContent(),c.getId());
	}



	public void delete(String id) {
		String sql="DELETE FROM `t_contact` WHERE id=?";
		db.executeSQL(sql, id);
	}



	
}
